﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2011
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Entities.Modules.Definitions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs
Imports DotNetNuke.Services.Log.EventLog
Imports DotNetNuke.Services.Upgrade

Namespace DotNetNuke.Providers.RadEditorProvider

    Public Class UpgradeController
        Implements IUpgradeable

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="Version"></param>
        ''' <returns></returns>
        ''' <remarks>This is not localizing Page Name or description.</remarks>
        Public Function UpgradeModule(ByVal Version As String) As String Implements IUpgradeable.UpgradeModule
            Try
                If Version = "06.00.00" Then
                    Dim resourceFile As String = "~/DesktopModules/Admin/RadEditorProvider/App_LocalResources/ProviderConfig.ascx.resx"
                    Dim pageName As String = Localization.GetString("HTMLEditorPageName", resourceFile)
                    Dim pageDescription As String = Localization.GetString("HTMLEditorPageDescription", resourceFile)

                    'Create Rad Editor Config Page (or get existing one)
                    Dim newPage As TabInfo = Upgrade.AddHostPage(pageName, pageDescription, "~/DesktopModules/Admin/RadEditorProvider/images/radeditor_config_small.png", "~/DesktopModules/Admin/RadEditorProvider/images/radeditor_config_large.png", True)

                    'Add Module To Page
                    Dim moduleDefId As Integer = GetModuleDefinitionID()
                    Upgrade.AddModuleToPage(newPage, moduleDefId, pageName, "~/DesktopModules/Admin/RadEditorProvider/images/radeditor_config_large.png", True)

                    For Each item In DesktopModuleController.GetDesktopModules(Null.NullInteger)
                        Dim moduleInfo As DesktopModuleInfo = item.Value

                        If moduleInfo.ModuleName = "DotNetNuke.RadEditorProvider" Then
                            moduleInfo.Category = "Host"
                            DesktopModuleController.SaveDesktopModule(moduleInfo, False, False)
                        End If
                    Next

                End If
            Catch ex As Exception
                Dim xlc As New ExceptionLogController
                xlc.AddLog(ex)

                Return "Failed"
            End Try

            Return "Success"
        End Function

        Private Function GetModuleDefinitionID() As Integer
            ' get desktop module
            Dim desktopModule As DesktopModuleInfo = DesktopModuleController.GetDesktopModuleByModuleName("DotNetNuke.RadEditorProvider", Null.NullInteger)
            If desktopModule Is Nothing Then
                Return -1
            End If

            'get module definition
            Dim moduleDefinition As ModuleDefinitionInfo = ModuleDefinitionController.GetModuleDefinitionByFriendlyName("RadEditor Manager", desktopModule.DesktopModuleID)
            If moduleDefinition Is Nothing Then
                Return -1
            End If

            Return moduleDefinition.ModuleDefID
        End Function

    End Class

End Namespace